package MVC.Model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DBOperation {

    public DBOperation() {
    }

    public Connection getMyCon() throws ClassNotFoundException, SQLException {
        String Host = "";
        String UserName = "root";
        String Password = "";
        String DataBase = "travel";

        String url = "jdbc:mysql://localhost/" + DataBase + "?user=" + UserName + "&password=" + Password + "&useUnicode=yes&characterEncoding=UTF-8";
        String Driver = "com.mysql.jdbc.Driver";

        Connection cn = null;

        Class.forName(Driver);
        cn = (Connection) DriverManager.getConnection(url);

        if (cn != null) {
            System.out.println("Ket noi thanh cong!");
        } else {
            System.out.println("Ket noi that bai !");
        }

        return cn;
    }

    public boolean checkLogin(String username, String password) {
        boolean tmp = false;
        Statement stm = null;
        ResultSet rs = null;
        String sSQL = "SELECT * FROM user WHERE username=" + username + " and password=" + password;
        try {
            stm = getMyCon().createStatement();
            rs = stm.executeQuery(sSQL);

            if (rs.next()) {
                tmp = true;
            }
            rs.close();
            stm.close();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DBOperation.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(DBOperation.class.getName()).log(Level.SEVERE, null, ex);
        }

        return tmp;
    }
}
